package org.hq.tool;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 普通类获取 bean
 */
@Component
public final class GetSpringBean implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        GetSpringBean.context = applicationContext;
    }

    /**
     * 通过类名获取 spring bean
     * @param className 类名
     * @return spring bean
     * @param <T> 对象类型
     */
    public static <T> T getBean(Class<T> className) {
        return context.getBean(className);
    }

    /**
     * 通过 bean 名 获取 spring bean
     * @param beanName bean 名
     * @return bean Object
     */
    public static Object getBean(String beanName) {
        return context.getBean(beanName);
    }
}
